<?php

namespace AppBundle\Controller\Mock;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Util\Gateway\GatewayUtil;

/**
 * 模拟广发接口发送请求
 *
 * @author yihao
 */
class GatewayController extends Controller
{

    /**
     * @Route("/main")
     * @Method("POST")
     */
    public function mainAction(Request $request)
    {
        $content = $request->getContent();

        $xml = simplexml_load_string($content);

        $senderId = $xml->xpath('/soapenv:Envelope/soapenv:Header/gateway:HeadType/gateway:senderId');
        $senderSN = $xml->xpath('/soapenv:Envelope/soapenv:Header/gateway:HeadType/gateway:senderSN');
        $tradeCode = $xml->xpath('/soapenv:Envelope/soapenv:Header/gateway:HeadType/gateway:tradeCode');
        $senderDate = $xml->xpath('/soapenv:Envelope/soapenv:Header/gateway:HeadType/gateway:senderDate');
        $senderTime = $xml->xpath('/soapenv:Envelope/soapenv:Header/gateway:HeadType/gateway:senderTime');
//        $now = time();
//        $errorCode = null;
//        $errorMsg = null;


        $errorCode = '8888';
        $errorMsg = 'Not supported mock system or trade code';
        $data = array();

        $result = $this->renderXml($errorCode, $errorMsg, $data);

        $response = new Response($result);
        $response->headers->set('Content-Type', 'xml');

        return $response;
    }

    private function renderXml($errorCode = '0000', $errorMsg = 'success', array $data = array())
    {
        $xml = simplexml_load_string('<?xml version="1.0" encoding="GBK"?>
            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:gateway="http://www.agree.com.cn/GDBGateway">
                <soapenv:Header>
                    <gateway:HeadType>
                       <gateway:versionNo>1</gateway:versionNo>
                       <gateway:toEncrypt>0</gateway:toEncrypt>
                       <gateway:commCode>510001</gateway:commCode>
                       <gateway:commType>0</gateway:commType>
                       <gateway:receiverId>PCRM</gateway:receiverId>
                       <gateway:senderId>IOBS</gateway:senderId>
                       <gateway:senderSN>20160719100336636549</gateway:senderSN>
                       <gateway:senderDate>20160719</gateway:senderDate>
                       <gateway:senderTime>145302</gateway:senderTime>
                       <gateway:tradeCode>PM0306</gateway:tradeCode>
                       <gateway:gwErrorCode>01</gateway:gwErrorCode>
                       <gateway:gwErrorMessage>Success</gateway:gwErrorMessage>
                    </gateway:HeadType>
                </soapenv:Header>
                <soapenv:Body>
                    <gateway:NoAS400>
                       <gateway:field name="CHN">IOBS</gateway:field>
                       <gateway:field name="COD">0306</gateway:field>
                       <gateway:field name="DDT">20160719100336636549</gateway:field>
                       <gateway:field name="SDT">20160719</gateway:field>
                       <gateway:field name="STM">150121</gateway:field>
                       <gateway:field name="hostReturnCode">0000</gateway:field>
                       <gateway:field name="hostErrorMessage">SUCCESS</gateway:field>
                       <gateway:field name="totalRows">0</gateway:field>
                       <gateway:field name="totalPages">0</gateway:field>
                       <gateway:fie name="lnidIcoll">
                       <gateway:field name="loanProduct">xxxxx</gateway:field>
                       <gateway:field name="loanProductId">1101010</gateway:field></gateway:fie>
                       <gateway:fie name="lnidIcoll">
                       <gateway:field name="loanProduct">xxxxx</gateway:field>
                       <gateway:field name="loanProductId">1101020</gateway:field></gateway:fie>
                       <gateway:fie name="lnidIcoll">
                       <gateway:field name="loanProduct">xxxxx</gateway:field>
                       <gateway:field name="loanProductId">5010010</gateway:field></gateway:fie>
                       <gateway:fie name="lnidIcoll">
                       <gateway:field name="loanProduct">xxxxx</gateway:field>
                       <gateway:field name="loanProductId">5010020</gateway:field></gateway:fie>
                    </gateway:NoAS400>
                </soapenv:Body>
            </soapenv:Envelope>');

        $bodyVals = $xml->xpath('/soapenv:Envelope/soapenv:Body/gateway:NoAS400');
        $bodyVal = $bodyVals[0];

        $errorCodeNode = $bodyVal->addChild('gateway:field', $errorCode);
        $errorCodeNode->addAttribute('name', 'errorCode');
        $errorMsgNode = $bodyVal->addChild('gateway:field', $errorMsg);
        $errorMsgNode->addAttribute('name', 'errorMsg');

        foreach ($data as $key => $value) {
            $node = $bodyVal->addChild('gateway:field', $value);
            $node->addAttribute('name', $key);
        }

        return $xml->asXML();
    }

}
